/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package GuiTools;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;

import Storage.*;
import Settings.*;

/**
 * @author Christoph Schnack
 *
 */
public class EditSynonymsDialog extends JDialog{
	JDialog dialog;
	boolean aborted = true;
	Vector<TermPanel> entries = new Vector<TermPanel>();
	Vector<Term> result;
	JPanel centerPanel;
	Settings SET;
	Language LANG;
	TribuitFrame parentFrame;
	Vector<String> categories;
	TermClass termClass;
	
	public EditSynonymsDialog(TribuitFrame parentFrameArg, Settings setArg, TermClass tc, Vector<String> categoriesArg, Vector<Term> termsArg){
		super(parentFrameArg, setArg.getLanguage().getEdit()+" "+setArg.getLanguage().getMeaning(),true);
		SET = setArg;
		LANG = setArg.getLanguage();
		parentFrame = parentFrameArg;
		categories = categoriesArg;
		termClass = tc;
		dialog=this;
		
		centerPanel = new JPanel();
		centerPanel.setLayout(new BoxLayout(centerPanel,BoxLayout.Y_AXIS));
		if(termsArg!=null && !termsArg.isEmpty()){
			TermPanel e;
			for(int i=0;i<termsArg.size();i++){
				e = new TermPanel(SET,termClass,categories,LANG.getSynonym());
				e.setUpTerm(termsArg.elementAt(i));
				entries.add(e);
				centerPanel.add(e);
			}
		}
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(GuiTools.BGCOLOR);
		final JButton addBtn = new JButton("+");
		addBtn.setToolTipText("STRG + N");
		addBtn.addActionListener(new AddBtnListener());
		final JButton okBtn = new JButton("ok");
		okBtn.setToolTipText("STRG + ENTER");
		okBtn.addActionListener(new EsOkListener());
		final JButton cancelBtn = new JButton(LANG.getCancel());
		cancelBtn.setToolTipText("STRG + D");
		cancelBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				dialog.dispose();
			}
		});
		buttonPanel.add(addBtn);
		buttonPanel.add(okBtn);
		buttonPanel.add(cancelBtn);
		JScrollPane jsp = new JScrollPane(centerPanel);
		jsp.setPreferredSize(new Dimension(300,200));
		
		centerPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ctrl N"), "newSynonym");
		centerPanel.getActionMap().put("newSynonym", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				addBtn.doClick();
			}
		});
		centerPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ctrl ENTER"), "ok");
		centerPanel.getActionMap().put("ok", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				okBtn.doClick();
			}
		});
		centerPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ctrl D"), "cancel");
		centerPanel.getActionMap().put("cancel", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				cancelBtn.doClick();
			}
		});
		
		dialog.setFocusable(true);
		dialog.getContentPane().add(BorderLayout.NORTH,new Header(LANG.getSynonyms()));
		dialog.getContentPane().add(BorderLayout.CENTER,jsp);
		dialog.getContentPane().add(BorderLayout.SOUTH,buttonPanel);
		dialog.pack();
		dialog.setLocationRelativeTo(parentFrame);
		if(termsArg!=null && !termsArg.isEmpty()){
			if(termClass.hasPrefix())entries.lastElement().grabFocusArticle();
			else entries.lastElement().grabFocusMeaningTF();
		}
		dialog.setVisible(true);
	}
	
	class AddBtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			entries.add(new TermPanel(SET,termClass,categories,LANG.getSynonym()));
			centerPanel.add(entries.lastElement());
			if(termClass.hasPrefix())entries.lastElement().grabFocusArticle();
			else entries.lastElement().grabFocusMeaningTF();
		}
	}
	
	class EsOkListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			for(int i=0;i<entries.size();i++){
				TermPanel en = entries.elementAt(i);
				if(termClass.hasPrefix()){
					if(termClass.getPreFixVal()==null){
						if(!GuiTools.checkInput(en.getArticle())){
							JOptionPane.showMessageDialog(dialog,LANG.getNotThoseSigns()+GuiTools.NOTALLOWEDSIGNS,LANG.getAttention(),JOptionPane.WARNING_MESSAGE);
							return;
						}
					}
				}
				if(!GuiTools.checkInput(en.getMeaning())){
					JOptionPane.showMessageDialog(dialog,LANG.getNotThoseSigns()+GuiTools.NOTALLOWEDSIGNS,LANG.getAttention(),JOptionPane.WARNING_MESSAGE);
					return;
				}
			} 
			Vector<Term> tmpSyns = new Vector<Term>();
			TermPanel en;
			for(int i=0;i<entries.size();i++){
				en = entries.elementAt(i);
				if(en.getMeaning().equals(""))continue;
				else{
					String article="",meaning=en.getMeaning();
					if(termClass.hasPrefix()) article=en.getArticle();
					if(tmpSyns.size()>0){
						for(Term existing : tmpSyns){
							if(existing.getTerm().equals(meaning)){
								JOptionPane.showMessageDialog(dialog,LANG.getSynonymAlreadyExists(),LANG.getAttention(),JOptionPane.WARNING_MESSAGE);
								en.mark();
								return;
							}
						}
					}
					tmpSyns.add(new Term(article,meaning,(String) en.getCategory()));
				}
			}
			aborted = false;
			result = tmpSyns;
			dialog.dispose();
		}
	}
	
	public boolean getAborted(){
		return aborted;
	}
	
	public Vector<Term> getResult(){
		return result;
	}
}
